$(document).ready(function(){
    $('.word').click(function(){
        var the_id = $(this).attr('id');
        var type = the_id.charAt(0);
        var span = $('#' + the_id + ' > span');
        var class_name = span.attr('class');
        if (class_name == 'hide') class_name = 'show';
        else class_name = 'hide';
        span.attr('class', class_name);
        // change show/hide mode for this part
        var mode = swap(type);
        if (mode != class_name) swap(type);
    });

    $('.word_operation').click(function(){
        var the_id = $(this).attr('id');
        var index = the_id.split('_');
        index = index[index.length - 1];
        modal_id = index;//global for modal dialog
        index = index - 1;
        var word_index = keys_shf[index];
        $(".modal-body").html(day_words[word_index].note);
        $(".word-operation-modal").modal("show");
    });

    $('#btn_learned_bookmark').click(function(){
        var index = modal_id;
        var word_index = keys_shf[index - 1];
//        index = the_id.split('_');
//        index = index[index.length - 1];
//        index = index - 1;
//        word_index = keys_shf[index];
        day_word_count = day_words[word_index].learned;
        $('#row_word_' + index).removeClass('word_row-' + day_word_count);
        day_words[word_index].learned = (day_word_count + 1) % 7;
        $('#row_word_' + index).addClass('word_row-' + day_words[word_index].learned);
        $(".word-operation-modal").modal("hide");
    });

    $('.display_mode').click(function(){
        var the_id = $(this).attr('id');
        var part = the_id.charAt(0);
        if (part == 'a') {
//            var index = the_id.split('_')[2];
//            if (typeof index == 'undefined') {
//                index = 0;
//            }
            show_hide_all();
        } else {
            show_hide(part);
        }
    });
});
function swap(part) {
    var mode;
    if (part == 'e') {
        if (e == 'show') e = 'hide';
        else e = 'show';
        mode = e;
    } else if (part == 'c') {
        if (c == 'show') c = 'hide';
        else c = 'show';
        mode = c;
    } else if (part == 'p') {
        if (p == 'show') p = 'hide';
        else p = 'show';
        mode = p;
    } else if (part == 'v') {
        if (v == 'show') v = 'hide';
        else v = 'show';
        mode = v;
    }
    set_mode_a(mode);
    return mode;
}
function get_mode(part) {
    var mode;
    if (part == 'e') {
        mode = e;
    } else if (part == 'c') {
        mode = c;
    } else if (part == 'p') {
        mode = p;
    } else if (part == 'v') {
        mode = v;
    } else {
        mode = a;
    }
    return mode;
}
function shuffle(array) {
    var currentIndex = array.length
        , temporaryValue
        , randomIndex
        ;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }
    return array;
}
function set_mode_a(mode) {
    a = mode;
    if (a == 'show') {
        $('#a_show > span').removeClass('glyphicon-list');
        $('#a_show > span').addClass('glyphicon-sort');
    } else {
        $('#a_show > span').removeClass('glyphicon-sort');
        $('#a_show > span').addClass('glyphicon-list');
    }
}
function reset_learned_layout() {
    $('.word_row').each(function(){
        $(this).attr('class', 'word_row');
    });
}
function clean_day_word_learned () {
    for (i = 0; i < day_words.length; i++) {
        day_words[i].learned = 0;
    }
}
function set_learned_layout() {
    for (i = 0; i < day_words.length; i++) {
        var row = $('#row_word_' + (i + 1));
        word_index = keys_shf[i];
        row.addClass('word_row-' + day_words[word_index].learned);
    }
}